<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class TableController extends Controller
{
    public function tablelist()
    {
         $data = Db::name('table')
                ->field(['id', 'number', 'count', 'status'])
                ->select();
        return view('table/table-list',[
            'data' => $data
        ]);
    }

    public function add()
    {
        return view('table/table-add');
    }

    public function save(Request $request)
    {
        $q = $request->post();
         $data = [
            'number' => $q['number'],
            'count' => $q['count']
        ];

        $result = db('table')->data($data)->insert();
       // var_dump($result);die;
        // 判断 执行情况
        if ($result > 0) {
            return $this->success('添加成功！', url('admin/TableController/tablelist'));
        } else {
            return $this->error('添加失败！');
        }

    }

    public function edit($id)
    {
        // var_dump($id);die;
        $data = Db::table('table')
                    ->field('id, number, count')
                    ->where('id', $id)
                    ->find();

        // var_dump($data);die;
        return view('table/table-edit',[
                'data' => $data
            ]);
    }

    public function update(Request $request,$id)
    {
        $q = $request->post();
        unset($q['_method']);

        $result = db('table')->where('id', $id)->update($q);
        // 判断 执行情况
        // var_dump($result);die;
        if ($result > 0) {
            return $this->success('编辑成功 !', url('admin/TableController/tablelist'));
        } else {
            return $this->error('编辑失败 !');
        }
    }

      public function delete($id)
    {
        // var_dump($id);die;
        $result = Db::name('table')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除失败!';
        }
        // JSON 返回
        return json($info);
    }

    public function tablecont()
    {
        $data = Db::name('table')
                ->field([ 'name','tel', 'member', 'time','number'])
                ->where('status','2')
                ->select();
        return view('table/tablecont-list',[
            'data' => $data
        ]);
    }


    public function details($id)
    {
        $data = Db::table('table')
                    ->field('*')
                    ->where('id', $id)
                    ->select();

        // var_dump($data);die;
        return view('table/table-details',[
                'data' => $data
            ]);
    }
}




















